package cn.dnuihyy406.dnuifood_16110100406.controller.searchfragment;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

import cn.dnuihyy406.dnuifood_16110100406.R;
import cn.dnuihyy406.dnuifood_16110100406.bean.FoodBean;
import cn.dnuihyy406.dnuifood_16110100406.controller.foodlist.FoodListAdapter;
import cn.dnuihyy406.dnuifood_16110100406.listener.OnRetrofitListener;
import cn.dnuihyy406.dnuifood_16110100406.model.SearchModel;
import cn.dnuihyy406.dnuifood_16110100406.model.ShopModel;

public class SearchResultActivity extends AppCompatActivity implements OnRetrofitListener<Object> {
    private RecyclerView recyclerView;
    private String search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);
        initViews();
        initData();
        initViews();
    }


    void initViews() {

        recyclerView = (RecyclerView) findViewById(R.id.rv3);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    void initEvents() { //处理返回按钮
    }

    void initData() {
        Intent intent = getIntent();
        search = intent.getStringExtra("search");
        SearchModel searchModel=new SearchModel();
        searchModel.getFoodBySearch(search,this);
    }


    @Override
    public void onSuccess(Object object, int flag) {
        if ((List)object != null && ((List) object).size() > 0) {
            FoodListAdapter adapter = new FoodListAdapter(SearchResultActivity.this,(List)object,R.layout.food_list_item);
            recyclerView.setAdapter(adapter);
        } else {
            Toast.makeText(this, "没有搜索结果", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(SearchResultActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

}